import { query } from '../services/home';
import { parse } from 'qs';
import { getCurrentMonth } from '../utils/tools';
import * as global from '../global';
import moment from 'moment';

const namespace = 'home';
export default {
  namespace,
  state: { data: [], loading: true ,name:'加载中...'},
  subscriptions:
  {
    setup({ dispatch, history }) {
      history.listen(location => {
        let month = getCurrentMonth();
          dispatch({
            type: 'query',
            payload: {
              auth_user: '',
              wid: global.appdata.wid,
              period: month
            }
          });
      });
    }
  },

  effects:
  {
    *query({ payload }, { call, put }) {
      yield put({ type: 'showLoading' });
      const { data } = yield call(query, parse(payload));
      if (data.result == 'success') {
      let pase_data = {
        name:data.detail[20].name,
        data:{
          bank:data.detail[1].value,
          cash:data.detail[2].value,
          income:data.detail[3].value,
          outlay:data.detail[4].value
        }
      };
        yield put({
          type: 'querySuccess',
          payload: {...pase_data}
        });
      }
    }
  },

  reducers: {
    querySuccess(state, action) {
      return { ...state, ...action.payload, loading: false };
    },
    hideLoading(state) {
      return {...state, loading: false };
    },
    showLoading(state) {
      return {...state, loading: true };
    }
  }
}